﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SolutionModel;
using NLite.Data;
using NUnit.Framework;
using System.Data;
using System.Data.SqlClient;
using System.Linq.Expressions;
using SFast;

namespace SolutionDAL
{
    [TestFixture]
    public class CommonClassDAL
    {
        #region 通过类型获取分类树

        /// <summary>
        /// 通过类型获取分类树
        /// </summary>
        /// <param name="ccType">分类类型</param>
        /// <returns></returns>
        public List<CommonClass> GetClassByType(CommonClassType ccType)
        {
            PurviewDBEntities db = DBConfig.GetDBContext();
            List<CommonClass> comCls = null;
            String type = ccType.ToString();
            db.UsingTransaction(() =>
            {
                comCls = db.CommonClasses.Where(o => o.CCType.Equals(type,StringComparison.CurrentCultureIgnoreCase)).ToList<CommonClass>();
            });
            return comCls;
        }
        #endregion


        #region 更新单体分类
        /// <summary>
        /// 更新单体分类
        /// </summary>
        /// <param name="commonClass">数据实例</param>
        /// <returns></returns>
        public int Update(CommonClass commonClass)
        {
            PurviewDBEntities db = DBConfig.GetDBContext();
            int rs = 0;
            db.UsingTransaction(() =>
            {
                rs = db.DbHelper.ExecuteNonQuery("exec spCommonClassUpdate @C_ID,@C_Parent_ID,@C_Order,@C_Name,@C_EnName,@CCType",
                new
                {
                    C_ID = commonClass.CCID,
                    C_Parent_ID = commonClass.CCPID,
                    C_Name = commonClass.CCName._HasVal(""),
                    C_Order = commonClass.CCID,
                    C_EnName = commonClass.CCEnName._HasVal(""),
                    CCType = commonClass.CCType
                }
                );
            });
            return rs;
        }
        #endregion

        #region 插入单体分类
        /// <summary>
        /// 插入单体
        /// </summary>
        /// <param name="list">新对象</param>
        /// <returns>返回新的ID</returns>
        public int Insert(CommonClass commonClass)
        {
            PurviewDBEntities dbe = DBConfig.GetDBContext();
            int rs = 0;
            dbe.UsingTransaction(() =>
            {
                rs = dbe.DbHelper.ExecuteNonQuery(" exec spCommonClassInsert @Pid,@Name,@EnName,@CCType",
                        new
                        {
                            Pid = commonClass.CCPID,
                            Name = commonClass.CCName,
                            EnName = commonClass.CCEnName,
                            CCType = commonClass.CCType
                        });

            });
            return rs;
        }
        #endregion


        //以下代码无用

        #region 批量插入分类
        /// <summary>
        /// 批量插入
        /// </summary>
        /// <param name="list">对象集合</param>
        /// <returns>返回新的ID</returns>
        public int Insert(List<CommonClass> list)
        {
            PurviewDBEntities dbe = DBConfig.GetDBContext();
            int rs = 0;
            dbe.UsingTransaction(() =>
            {
                list.ForEach(o =>
                {
                    rs = dbe.CommonClasses.Insert(o, c => c.CCID);
                });
            });
            return rs;
        }
        #endregion

        #region 批量更新分类
        /// <summary>
        /// 批量更新
        /// </summary>
        /// <param name="list">对象集合</param>
        /// <returns>返回新的ID</returns>
        public int Update(List<CommonClass> list)
        {
            PurviewDBEntities dbe = DBConfig.GetDBContext();
            int rs = 0;
            dbe.UsingTransaction(() =>
            {
                list.ForEach(o =>
                {
                    rs = dbe.CommonClasses.Update(o);
                });
            });
            return rs;
        }
        #endregion

        #region 删除分类
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="predicate">删除表达式</param>
        /// <returns></returns>
        public int Delete(Expression<Func<CommonClass, bool>> predicate)
        {
            PurviewDBEntities db = DBConfig.GetDBContext();
            int rs = db.CommonClasses.Delete(predicate);
            return rs;
        }
        #endregion

        #region 获取所有分类
        /// <summary>
        /// 获取所有
        /// </summary>
        /// <returns></returns>
        public IDbSet GetAll()
        {
            PurviewDBEntities db = DBConfig.GetDBContext();
            return db.CommonClasses;
        }
        #endregion
    }
}
